package com.vq3.digitalizationsoftwareintegrates.Servlet.BusinessCenter;


import com.vq3.digitalizationsoftwareintegrates.Entity.BusinessCenter.BusinessCenterEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.BusinessCenter.BusinessCenterEntity_perEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.DataCenter.DataCenterEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.DataCenter.DataCenter_permissionEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Info.ResultInfo;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.User_RoleEntity;
import com.vq3.digitalizationsoftwareintegrates.Service.BusinessCenter.BusinessCenterService;
import com.vq3.digitalizationsoftwareintegrates.Service.Index.IndexSoftwareService;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.Impl.PackagedDataImpl;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.PackagedData;
import com.vq3.digitalizationsoftwareintegrates.Utils.PermissionControl.GetUserPermission;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("businessCenter")
public class BusinessCenterServlet {
    @Resource
    BusinessCenterService businessCenterService;
    @Resource
    GetUserPermission getUserPermission;

    @RequestMapping("findBusiness")
    public void findBusiness(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {


        ResultInfo resultInfo = new ResultInfo();
        PackagedData packagedData = new PackagedDataImpl();
        List<BusinessCenterEntity> dataCenterSideBar = new ArrayList<>();
        resultInfo.setFlag(false);

        UserEntity userEntity = (UserEntity)httpServletRequest.getSession().getAttribute("USER");
        List<Permission_TBEntity> permission_tbEntities = getUserPermission.GetPermission(userEntity.getUname());
        Set<Integer> BidS = new HashSet<>();
        if (!permission_tbEntities.isEmpty()){
            for (Permission_TBEntity permission_tbEntity : permission_tbEntities) {
                List<BusinessCenterEntity_perEntity> businessCenterEntity_perEntities = businessCenterService.perFindBusinessCenterId(permission_tbEntity);
                if (!businessCenterEntity_perEntities.isEmpty()) {
                    for (BusinessCenterEntity_perEntity dataCenter_permissionEntity : businessCenterEntity_perEntities) {
                        BidS.add(dataCenter_permissionEntity.getBid());
                    }
                }
            }
        }
        for (Integer bid : BidS) {
            BusinessCenterEntity dataCenterEntity = businessCenterService.findBusinessCenterSideBar(bid);
            if (dataCenterEntity != null) {
                dataCenterSideBar.add(dataCenterEntity);
            }
        }
        if (!dataCenterSideBar.isEmpty()) {
            resultInfo.setFlag(true);
            resultInfo.setData(dataCenterSideBar);
        }
        packagedData.writeValue(resultInfo,httpServletResponse);
    }
}
